/******
 * waitpid.c - Simple wait usage
 *********/
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
  int i;
  int j[2];
} sample_str;


int main(void)
{

  sample_str a, b;

  a.i = 123;
  a.j[0] = 456;
  a.j[1] = 789;

  b = a;

  printf("a:%d, %d\n", a.i, a.j[0]);
  printf("b:%d, %d\n", b.i, b.j[0]);

  a.i = 1234;
  a.j[0] = 7890;

  printf("a:%d, %d\n", a.i, a.j[0]);
  printf("b:%d, %d\n", b.i, b.j[0]);
  printf("%p, %p\n", &a, &b);

  return 0;

  char *p[3];
  p[0] = "0000000";
  p[1] = "1111111";
  p[2] = "2222222";

  int i;
  for (i = 0; i < 3; i++)
  {
    printf("(%s)\n", *(p + i));
  }

  return 0;

  pid_t childpid;
  int status;
  childpid = fork();
  if (childpid < 0)
  {
    perror("fork()");
    exit(EXIT_FAILURE);
  } else if (childpid == 0)
  {
    sleep(3);  //让子进程睡眠3秒，看看父进程的行为
    puts("In child process");
    printf("\tchild pid = %d\n", getpid());
    printf("\tchild ppid = %d\n", getppid());
    exit(EXIT_SUCCESS);
  } else
  {
    waitpid(childpid, &status, 0);

    int result = 0;
    if (WIFEXITED(status))
    {
      result = WEXITSTATUS(status);
    }

    puts("in parent");
    printf("\tparent pid = %d\n", getpid());
    printf("\tparent ppid = %d\n", getppid());
    printf("\tchild process exited with status %d \n", status);
    printf("\tchild process exited with status code %d \n", result);
  }
  exit(EXIT_SUCCESS);
}
